package com.xl.bigdata.es.index;

import com.xl.bigdata.es.xpack.ESXpackRestHighLevelClient;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.search.fetch.subphase.FetchSourceContext;

import java.io.IOException;

/**
 * @ClassName GetAPI
 * @Description TODO 本质是调用refresh，生成segment，是索引可搜索
 * @Author xl.gao
 * @Date 2021/12/30 15:32
 * @Version
 */
public class GetAPI {

    /**
     * @Description: 通用方法
     * @Author: xl.gao
     * @Date:
     * @param client:
     * @param index:
     * @param id:
     * @return: java.lang.String
     **/
    public static String getAPI(RestHighLevelClient client, String index, String id) {

        GetRequest getRequest = new GetRequest(index, id);

        try {
            GetResponse documentFields = client.get(getRequest, RequestOptions.DEFAULT);

            if (documentFields.isExists()) {

                return documentFields.getSourceAsString();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        return "";
    }

    /**
     * @Description: 只获取ID
     * @Author: xl.gao
     * @Date:
     * @param client:
     * @param index:
     * @param id:
     * @param noSource: True不返回source
     * @return: java.lang.String  只返回ID
     **/
    public static String getAPI(RestHighLevelClient client, String index, String id, boolean noSource) {

        GetRequest getRequest = new GetRequest(index, id);

        // 设置响应不返回任何字段
        if(noSource){
            getRequest.fetchSourceContext(FetchSourceContext.DO_NOT_FETCH_SOURCE);
        }

        try {
            GetResponse documentFields = client.get(getRequest, RequestOptions.DEFAULT);

            if (documentFields.isExists()) {

                return documentFields.getId();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        return "";
    }

    /**
     * @Description: 判断ID是否存在
     * @Author: xl.gao
     * @Date:
     * @param client:
     * @param index:
     * @param id:
     * @return: boolean
     **/
    public static boolean getExistsAPI(RestHighLevelClient client, String index, String id){

        GetRequest getRequest = new GetRequest(index, id);

        // 设置source不返回
        getRequest.fetchSourceContext(FetchSourceContext.DO_NOT_FETCH_SOURCE);
        // 设置field不返回
        getRequest.storedFields("_none_");

        try {
            boolean exists = client.exists(getRequest, RequestOptions.DEFAULT);

            return exists;
        } catch (IOException e) {
            e.printStackTrace();
        }

        return false;
    }

    public static void main(String[] args) {
        RestHighLevelClient restHighLevelClient = ESXpackRestHighLevelClient.getRestHighLevelClient();

        getAPI(restHighLevelClient, "address_base", "test_index_api_02");

        ESXpackRestHighLevelClient.closeRestHighLevelClient();
    }
}
